package com.fieldchain.ex.fa100.ui.personal.activity;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.bean.BankBean;
import com.fieldchain.ex.fa100.inter.IntegerCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 123 on 2018/4/19.
 */

public class BankAdapter extends RecyclerView.Adapter<BankAdapter.MyViewHolder> {
    private List<BankBean> list = new ArrayList<>();
    private Context mContext;
    private String userid;
    private int oldIndex = -1, newIndex = -1;
    private IntegerCallBack callBack;
    private boolean tags[];

    public BankAdapter(Context context) {
        this.mContext = context;
    }

    public void setIntegerCallBack(IntegerCallBack callBack) {
        this.callBack = callBack;
    }

    public void setData(List<BankBean> list) {
        this.list.clear();
        this.list.addAll(list);
        tags = new boolean[list.size()];
        notifyDataSetChanged();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_bank, parent, false);
        MyViewHolder ViewHolder = new MyViewHolder(inflate);
        return ViewHolder;
    }


    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final BankBean bean = list.get(position);

        if (position == 0 && oldIndex == -1) {
            holder.cbox.setChecked(true);
        } else if (tags[position]) {
            holder.cbox.setChecked(true);
        } else {
            holder.cbox.setChecked(false);
        }

        holder.tvBankname.setText(bean.getBank());
        String card=bean.getBankCard();
        if(card!=null) {
            holder.tvBankcard.setText(card.substring(card.length() - 5, card.length() - 1));
        }
        holder.linBank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.cbox.setChecked(true);
                newIndex = position;
                tags[newIndex] = true;
                if (oldIndex != -1) {
                    tags[oldIndex] = false;
                    notifyItemChanged(oldIndex);
                }
                oldIndex = newIndex;
                if (callBack != null) {
                    callBack.index(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout linBank;
        private CheckBox cbox;
        private TextView tvBankcard;
        private TextView tvBankname;

        public MyViewHolder(View itemView) {
            super(itemView);
            linBank = itemView.findViewById(R.id.lin_bank);
            cbox = itemView.findViewById(R.id.cbox);
            tvBankcard = itemView.findViewById(R.id.tv_bankcard);
            tvBankname = itemView.findViewById(R.id.tv_bankname);
        }
    }
}
